<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>Class::DBI 改 Primary 项</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Atom.html">如何创建 Atom</a>&nbsp;&nbsp;>>Next: <a href="050401.html">Win32 下 Apache 和 #!/usr/bin/perl</a></p>
<h1>Class::DBI 改 Primary 项</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>Class::DBI</b></p><h2>问题</h2>
今天碰到的是在数据库里更改一项 primary auto_increment field.<br>
Class::DBI 中的 move 好像失效了。在 DBI.pm 中是这么写的。
<pre><code>sub move {
    my ($class, $old_obj, @data) = @_;
    $class->_carp("move() is deprecated. If you really need it, "
            . "you should tell me quickly so I can abandon my plan to remove it.");
    return $old_obj->_croak("Can't move to an unrelated class")
        unless $class->isa(ref $old_obj)
        or $old_obj->isa($class);
    return $class->create($old_obj->_data_hash(@data));
}</code></pre>
而我试了下 set( 'cms_id' => $new_id); update 也不成。在 POD 文档中是这么说的。
<pre>The update() method returns the number of rows updated, which should
always be 1, or else -1 if no update was needed. If the record in the
database has been deleted, or its primary key value changed, then the
update will not affect any records and so the update() method will
return 0.
方法 update() 返回更新的行数，应该经常是 1, 或者没有必要的更新时返回 -1. 
如果数据库中的纪录被删除，或者说它的主键被更改，那此后的更新不会再影响任何纪录，所以 update() 将返回 0.</pre>
最后到 <a href='http://www.class-dbi.com/cgi-bin/wiki/index.cgi'>CDBI wiki</a> 上找了个 <a href='http://www.class-dbi.com/cgi-bin/wiki/index.cgi?DirectlyExecuteSql'>DirectlyExecuteSql</a>.<br>
最后写了代码如下：
<pre><code>
my $new_id = Eplanet::M::CDBI::Cms->maximum_value_of('cms_id');
$new_id++;

my $dbh = Eplanet::M::CDBI::Cms->db_Main();
    $dbh->do(qq|
    UPDATE cms SET cms_id = $new_id WHERE cms_id = $id
|);
</code></pre></div>
<p><&lt;Previous: <a href="Atom.html">如何创建 Atom</a>&nbsp;&nbsp;>>Next: <a href="050401.html">Win32 下 Apache 和 #!/usr/bin/perl</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=Class::DBI%20%E6%94%B9%20Primary%20%E9%A1%B9&url=http://www.fayland.org/journal/050330.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='ShareURL0503.html'>Share the URLs with u (Mar. 2005)</a> < <span class='digit'>2005-03-08 00:36:53</span> ></li><li><a href='ShareURL0506.html'>Share the URLs (June 2005)</a> < <span class='digit'>2005-06-01 23:51:11</span> ></li><li><a href='ShareURL0504.html'>Share the URLs with u (Apr. 2005)</a> < <span class='digit'>2005-04-01 10:18:51</span> ></li><li><a href='ShareURL0505.html'>Share the URLs with u (May 2005)</a> < <span class='digit'>2005-05-10 22:16:17</span> ></li><li><a href='ShareURL0509.html'>Share the URLs (September 2005)</a> < <span class='digit'>2005-09-22 12:29:39</span> ></li><li><a href='ShareURL0510.html'>Share the URLs (October 2005)</a> < <span class='digit'>2005-10-08 10:38:10</span> ></li><li><a href='ShareURL0512.html'>Share the URLs (December 2005)</a> < <span class='digit'>2005-12-01 00:13:53</span> ></li></ul>
Created on <span class="digit">2005-03-30 22:27:07</span>, Last modified on <span class="digit">2005-05-20 23:56:36</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>